{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit RegHelper;

interface

uses Registry;

const
  AHM_BASE_KEY = '\Software\@home media';

type
  TAHMRegistryHelper = class
  private
    FRootKey: String;
    FRegistry: TRegistry;
  protected
    procedure SetRootKey(Key: String);
  public
    constructor Create;
    destructor Destroy; override;
    property RootKey: String read FRootKey write SetRootKey;
    procedure WriteStrValue(Key: String; Value: String);
    function ReadStrValue(Key: String): String;
  end;

implementation

uses Windows, Forms;

constructor TAHMRegistryHelper.Create;
begin
  inherited;

  FRegistry := TRegistry.Create;
  FRegistry.RootKey := HKEY_CURRENT_USER;

  FRegistry.OpenKey(AHM_BASE_KEY, True);

  // Set default root key for application
  SetRootKey(Application.Title);
end;

destructor TAHMRegistryHelper.Destroy;
begin
  FRegistry.CloseKey;
  FRegistry.Free;

  inherited;
end;

procedure TAHMRegistryHelper.SetRootKey(Key: String);
begin
  if (FRootKey <> Key) and (Key <> '') then
  begin
    FRegistry.CloseKey;
    if FRegistry.OpenKey(AHM_BASE_KEY + '\' + Key, True) then
      FRootKey := Key;
  end;
end;

procedure TAHMRegistryHelper.WriteStrValue(Key: String; Value: String);
begin
  FRegistry.WriteString(Key, Value);
end;

function TAHMRegistryHelper.ReadStrValue(Key: String): String;
begin
  Result := FRegistry.ReadString(Key);
end;

end.
